package bairui.international.service.impl;

import java.util.List;

import javax.persistence.LockModeType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lianjing.sh.validate.spring.exception.ValidateObjectException;
import com.sh_lianjing.state.StateConvertException;

import bairui.international.model.domain.jpa.TB_BranchCompany;
import bairui.international.model.domain.jpa.TB_BranchCompanyStaff;
import bairui.international.model.domain.jpa.TB_Town;
import bairui.international.model.domain.jpa.TB_User;
import bairui.international.service.Service_BranchCompany;
import bairui.international.service.Service_BranchCompanyStaff;
import bairui.international.service.Service_Town;
import bairui.international.service.Service_User;

/**
 * 
 * 【(分公司)TB_BranchCompany】服务实现
 *
 * @generated
 * 
 * @see bairui.international.model.domain.jpa.TB_BranchCompany 模型
 * @see Service_BranchCompany 服务接口
 * @see BService_BranchCompanyImpl 服务实现基础
 *
 */
@Service
public class Service_BranchCompanyImpl extends BService_BranchCompanyImpl implements Service_BranchCompany {

	@Autowired Service_Town service_Town;
	@Autowired Service_User service_User;
	@Autowired Service_BranchCompanyStaff service_BranchCompanyStaff;
	
	@Override
	public void publish(long id) throws StateConvertException {
		TB_BranchCompany bean = findById(id);
		bean.setState(bean.getState().t_publish_from_Draft().to());
	}

	@Override
	public void down(long id) throws StateConvertException {
		TB_BranchCompany bean = findById(id);
		bean.setState(bean.getState().t_down_from_Uped().to());
	}

	@Override
	public void up(long id) throws StateConvertException {
		TB_BranchCompany bean = findById(id);
		bean.setState(bean.getState().t_up_from_Downed().to());
	}

	@Override
	public void addArea(long id, TB_Town town) throws Exception {
		TB_BranchCompany bean = findById(id);
		town = service_Town.load(town);
		if(null != town.getBranchCompany()){
			throw new ValidateObjectException("object", "", "区域已经被分配");
		}
		//
		bean.addArea(town);
	}

	@Override
	public void addStaff(long id, TB_User user) throws Exception {
		TB_BranchCompany bean = findById(id);
		user = service_User.load(user);
		List<TB_BranchCompanyStaff> branchCompanyStaffs = user.getBranchCompanyStaffs();
		// 验证
		for(TB_BranchCompanyStaff item : branchCompanyStaffs){
			if(item.getActiveFlag()){
				throw new ValidateObjectException("object", "", "用户经被分配");
			}
		}
		// 处理
		for(TB_BranchCompanyStaff item : branchCompanyStaffs){
			if(item.getBranchCompany().equals(bean)){
				item.setActiveFlag(true);
				return;
			}
		}
		//
		service_BranchCompanyStaff.create(bean, user);
	}

	@Override
	public void removeStaff(TB_BranchCompanyStaff staff) throws Exception {
		staff = service_BranchCompanyStaff.load(staff);
		staff.setActiveFlag(false);
	}

	@Override
	public void addArea(long id, List<TB_Town> towns) throws Exception {
		for(TB_Town town : towns){
			addArea(id, town);
		}
	}

	@Override
	public void addStaff(long id, List<TB_User> users) throws Exception {
		for(TB_User user : users){
			addStaff(id, user);
		}
	}
	
	/**
	 * 通过名称查找（加锁）
	 */
	@Override
	public TB_BranchCompany findByIdUseLock(Long id){
		return dao
				.find(QRoot.id.eq(id))
				.setLockMode(LockModeType.PESSIMISTIC_READ)
				.oneResult();
	}
	
}
